package top.chukongxiang.dubboboot.simple.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtils {

    /***
     * 日期转换cron表达式时间格式
     * @param date
     * @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String getCron(Date date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }

    /***
     *  日期转换cron时间个数 ,eg.  "ss mm HH dd MM ? yyyy"
     * @param date 时间
     * @return
     */
    public static String getCron(Date date) {
        String dateFormat = "ss mm HH dd MM ? yyyy";
        return getCron(date, dateFormat);
    }

    public static final Map<String,SimpleDateFormat> simpleDateFormats = new HashMap<>();

    public static SimpleDateFormat getFormat(String pattern){
        SimpleDateFormat simpleDateFormat = simpleDateFormats.get(pattern);
        if(simpleDateFormat!=null) return simpleDateFormat;
        simpleDateFormat = new SimpleDateFormat(pattern);
        synchronized (simpleDateFormats) {
            simpleDateFormats.put(pattern, simpleDateFormat);
        }
        return simpleDateFormat;
    }

    public static DateTimeFormatter getFormater(String pattern){
        return DateTimeFormatter.ofPattern(pattern);
    }

    public static String getDate(){
        return getDate("yyyy-MM-dd HH:mm:ss");
    }

    public static String getDate(String pattern){
        SimpleDateFormat simpleDateFormat = getFormat(pattern);
        String format = simpleDateFormat.format(new Date());
        return format;
    }

    public static String getRangeString(String parrent,int delay,int amont){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(amont,delay);
        Date time = calendar.getTime();
        SimpleDateFormat simpleDateFormat = getFormat(parrent);
        return simpleDateFormat.format(time);
    }

    public static Date getRangeDate(Date start,int delay,int amont){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        calendar.add(amont,delay);
        return calendar.getTime();
    }

    public static Date getRangeDate(int delay,int amont){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(amont,delay);
        return calendar.getTime();
    }

    public static Date parseDate(String dateStr,String pattern){
        SimpleDateFormat simpleDateFormat = getFormat(pattern);
        try {
            Date date = simpleDateFormat.parse(dateStr);
            return date;
        } catch (ParseException e) {
            return null;
        }
    }

    public static Date parseDate(String dateStr){
        //解析dateStr，生成格式
        SimpleDateFormat simpleDateFormat = getFormat(getSimplePattern(dateStr));
        try {
            Date date = simpleDateFormat.parse(dateStr);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String format(Date date,String pattern){
        if (date==null) return null;
        return getFormat(pattern).format(date);
    }
    public static String format(Long dateTime) {
        return format(new Date(dateTime));
    }
    public static String format(String pattern){
        return format(new Date(), pattern);
    }
    public static String format(Date date){
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }

    // 获取当前年份
    public static String getYear(){
        return getYear(new Date());
    }
    // 获取当前月份
    public static String getMonth(){
        return getMonth(new Date());
    }
    // 获取当前日期
    public static String getDay(){
        return getDay(new Date());
    }

    public static String getYear(Date date){
        SimpleDateFormat simpleDateFormat = getFormat("yyyy");
        return simpleDateFormat.format(date);
    }
    public static String getMonth(Date date){
        SimpleDateFormat simpleDateFormat = getFormat("MM");
        return simpleDateFormat.format(date);
    }
    public static String getDay(Date date){
        SimpleDateFormat simpleDateFormat = getFormat("dd");
        return simpleDateFormat.format(date);
    }
    public static int getHour(Date date){
        SimpleDateFormat simpleDateFormat = getFormat("HH");
        return Integer.parseInt(simpleDateFormat.format(date));
    }
    public static int getMinute(Date date){
        SimpleDateFormat simpleDateFormat = getFormat("mm");
        return Integer.parseInt(simpleDateFormat.format(date));
    }

    //解析日期格式，传入日期，得到这个日期的格式串
    public static String getSimplePattern(String simpleDateStr){
        char[] chars = simpleDateStr.toCharArray();
        StringBuffer patternBuffer = new StringBuffer();
        boolean hasYear=false,hasMonth=false,hasDay=false,hasHour=false,hasMinute=false,hasSecond=false,hasMill=false;
        for(int i=0;i<chars.length;i++){
            if(!hasYear && chars[i]>47 && chars[i]<58 && chars[i+1]>47 && chars[i+1]<58 && chars[i+2]>47 && chars[i+2]<58 && chars[i+3]>47 && chars[i+3]<58 ){
                patternBuffer.append("yyyy");
                hasYear = true;
                i += 3;
            } else if(!hasMonth && ((chars[i]==48 && chars[i+1]>47 && chars[i+1]<58) || chars[i]==49 && (chars[i]==48 || chars[i]==49 || chars[i]==50))){
                patternBuffer.append("MM");
                hasMonth = true;
                i += 1;
            } else if( !hasDay && (chars[i]==48 || chars[i]==49 || chars[i]==50 || chars[i]==51) && (chars[i+1]>47 && chars[i+1]<58)){
                patternBuffer.append("dd");
                hasDay = true;
                i += 1;
            } else if(!hasHour && (chars[i]==48 || chars[i]==49 || chars[i]==50) && (chars[i+1]>47 && chars[i+1]<58)){
                patternBuffer.append("HH");
                hasHour = true;
                i+=1;
            } else if(!hasMinute && chars[i]>47 && chars[i]<54 && chars[i+1]>47 && chars[i+1]<58){
                patternBuffer.append("mm");
                hasMinute = true;
                i+=1;
            } else if(!hasSecond && chars[i]>47 && chars[i]<54 && chars[i+1]>47 && chars[i+1]<58){
                patternBuffer.append("ss");
                hasSecond = true;
                i+=1;
            } else if(!hasMill && chars[i]>47 && chars[i]<58 && chars[i+1]>47 && chars[i+1]<58 && chars[i+2]>47 && chars[i+2]<58){
                patternBuffer.append("SSS");
                hasMill = true;
                i += 2;
            } else {
                patternBuffer.append(chars[i]);
            }
        }
        return patternBuffer.toString();
    }


    public static boolean belongCalendar(Date nowTime, Date beginTime,
                                         Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }


    static Calendar sameDayCalender1 = Calendar.getInstance();
    static Calendar sameDayCalender2 = Calendar.getInstance();
    public static boolean isSameDay(Date date1, Date date2) {
        if(date1 != null && date2 != null) {
            sameDayCalender1.setTime(date1);
            sameDayCalender2.setTime(date2);
            return isSameDay(sameDayCalender1, sameDayCalender2);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }

    public static boolean isSameDay(Calendar cal1, Calendar cal2) {
        if(cal1 != null && cal2 != null) {
            return cal1.get(0) == cal2.get(0) && cal1.get(1) == cal2.get(1) && cal1.get(6) == cal2.get(6);
        } else {
            throw new IllegalArgumentException("The date must not be null");
        }
    }
    public static Date getByDateRange(Integer target){
        if (target==null) return null;
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, target);
        return calendar.getTime();
    }
}
